/**
 *
 */
package org.tim.collections;

import java.util.ArrayList;

/**
 * A very simple list that handles a default value
 *
 * @author Tim Fennis
 */
public class DefaultValueList<E> {
	
	private E def = null;
	private ArrayList<E> internal = new ArrayList<E>();
	
	/**
	 * Adds an item to the internal list, making it the default item if no default item is available
	 * Returns the index of the added object
	 * @param obj The object to add to the list
	 * @return the index of the added object
	 */
	public int add ( E obj ) {
		if ( def == null ) {
			def = obj;
		}
		internal.add(obj);
		return internal.indexOf(obj);
	}
	
	/**
	 * Return the object with the given index
	 * @param index The index of the requested object
	 * @return the object with the index supplied as paramter, or null
	 */
	public E get ( int index ) {
		return internal.get(index);
	}
	
	/**
	 * Remove an item from the list
	 * If the removed item was the default value, the default value for this object is reset to zero
	 * @param obj The object to remove
	 */
	public void remove ( E obj ) {
		if ( def.equals(obj) ) {
			def = null;
		}
		internal.remove(obj);	
	}
	
	/**
	 * Overwrite the default object for this list, the previous default object will not be deleted
	 *
	 * @param obj The new default object
	 */
	public void setDefault ( E obj ) {
		def = obj;
	}
	
	/**
	 * Returns the default object
	 * @retrun the default object
	 */
	public E getDefault ( ) {
		return def;
	}
}